package com.longge;


import java.util.Arrays;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-02-27 22:00
 */
public class ConstructMaximumBinaryTree {

    public TreeNode constructMaximumBinaryTree(int[] nums) {

        return dfs(nums, 0, nums.length);

    }

    private TreeNode dfs(int[] nums, int l, int r) {
        if (l>=r) return null;
        int max = -1;
        int middle = 0;
        for (int i = l; i <r; i++) {
            if (max < nums[i]) {
                max = nums[i];
                middle = i;
            }
        }
        TreeNode treeNode = new TreeNode(max);
        treeNode.left = dfs(nums, l, middle);
        treeNode.right = dfs(nums, middle+1, r);
        return treeNode;
    }
}
